#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int inf=1<<30;
const int N=10000;
int cnt=1,head[N],cur[N];
int n,m,s,t;
struct Node{
    int v;
    int next;
    int val;
}node[N*20];
inline void addedge(int u,int v,int val){
    node[++cnt].v=v;
    node[cnt].val=val;
    node[cnt].next=head[u];
    head[u]=cnt;
}
int dep[N],gap[N];
void bfs(){
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    dep[t]=0;
    gap[0]=1;
    queue<int>q;
    q.push(t);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=head[u];i;i=node[i].next){
            int v=node[i].v;
            if(dep[v]!=-1)continue;
            q.push(v);
            dep[v]=dep[u]+1;
            gap[dep[v]]++;
        }
    }
    return;
}
int maxflow;
int dfs(int u,int flow){
    if(u==t){
        maxflow+=flow;
        return flow;
    }
    int used=0;
    for(int i=cur[u];i;i=node[i].next){
        cur[u]=i;
        int d=node[i].v;
        if(node[i].val&&dep[d]+1==dep[u]){
            int mi=dfs(d,min(node[i].val,flow-used));
            if(mi){
                node[i].val-=mi;
                node[i^1].val+=mi;
                used+=mi;
            }
            if(used==flow)return used;
        }
    }
    --gap[dep[u]];
    if(gap[dep[u]]==0)dep[s]=n+1;
    dep[u]++;
    gap[dep[u]]++;
    return used; 
}
int ISAP(){
    maxflow=0;
    bfs();
    while(dep[s]<n){
        memcpy(cur,head,sizeof(head));
        dfs(s,inf);
    }
    return maxflow;
}
int main(){
    int i=1;cin>>n>>m>>s>>t;
    int u,v,w;
    for(i=1;i<=m;i++)cin>>u>>v>>w,addedge(u,v,w),addedge(v,u,0);  
    printf("%d",ISAP());
    return 0;
}